<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<title>合同列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit">
<%@ include file="/WEB-INF/common.jsp"%>

<link href="${path }/resources/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
<link href="${path }/resources/css/animate.css" rel="stylesheet">
<link href="${path }/resources/css/style.css?v=4.1.0" rel="stylesheet">

<link rel="stylesheet" type="text/css"
	href="${path }/resources/css/bootstrap.min.css">
<!--树形结构 css样式-->
<link rel="stylesheet" type="text/css"
	href="${path }/resources/css/bootstrap-treeview.min.css">
<!--dataTables css样式-->
<link rel="stylesheet" type="text/css"
	href="${path }/resources/css/jquery.dataTables.min.css">
<!--DataTables button扩展-->
<link rel="stylesheet" type="text/css"
	href="${path }/resources/css/buttons.dataTables.min.css">
<link rel="stylesheet" type="text/css"
	href="${path }/resources/css/form.css">


</head>
<body class="gray-bg" style="">
<jsp:include page="/WEB-INF/loginCheck.jsp" />
	<div>
		<h1 style="margin-left:20px">添加维保项目</h1>
		<hr>
	</div>
		<form class="form-horizontal form-label-left" >
			<div class="form-group">
				<label class="control-label col-md-3 col-sm-3 col-xs-12"
					for="last-name">项目名称<span class="required">:</span>
				</label>
				<div class="col-md-6 col-sm-6 col-xs-12">
					<input  value="${contract.projectName}" class="form-control col-md-7 col-xs-12" readonly>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label col-md-3 col-sm-3 col-xs-12"
					for="last-name">消防系统<span class="required">:</span>
				</label>
				<div class="col-md-6 col-sm-6 col-xs-12">
					<div id="tree" ></div> 
				</div>
			</div>
			<div class="form-group">
				<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-5" >
					<button class="btn btn-primary" type="button" onclick="javascript:window.history.back(-1);">返回</button>
					<button type="button" id="btn-save" class="btn btn-primary">保存</button>
				</div>
			</div>
		</form>
 	
	<script src="${path }/resources/js/jquery.min.js"></script>
	<!-- Peity-->
	<script src="${path }/resources/js/plugins/peity/jquery.peity.min.js"></script>
	
	<!-- Bootstrap table-->
	<script src="${path }/resources/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
	<script src="${path }/resources/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>

	<!-- 自定义js-->
	<script src="${path }/resources/js/content.js?v=1.0.0"></script>
	
	<!-- jQuery Validation plugin javascript-->
    <script src="${path }/resources/js/plugins/validate/jquery.validate.min.js"></script>
    <script src="${path }/resources/js/plugins/validate/messages_zh.min.js"></script>
   
   	<!-- jQuery form  -->
    <script src="${path }/resources/js/jquery.form.min.js"></script>
    
    <!-- layer javascript -->
    <script src="${path }/resources/js/plugins/layer/layer.min.js"></script>
    
    	<!--DataTables 核心 js-->
	<script src="${path }/resources/js/jquery.dataTables.min.js"></script>
	<!--DataTables button扩展-->
	<script
		src="${path }/resources/js/dataTables.buttons.min.js"></script>
	<!--DataTables 导出相关js文件-->
	<script src="${path }/resources/js/buttons.html5.min.js"></script>
	<script src="${path }/resources/js/jszip.min.js"></script>
	<!-- 省市联动 -->
	<script src="${path }/resources/js/distpicker.data.js"></script>
	<script src="${path }/resources/js/distpicker.js"></script>
	<script src="${path }/resources/js/main.js"></script>
	<!-- 树形结构插件 -->
	<script src="${path }/resources/js/bootstrap-treeview.js"></script>	
		<!-- 等待界面的引用 -->
	<script type="text/javascript"
		src="http://malsup.github.io/min/jquery.blockUI.min.js"></script>
    
	<script type="text/javascript">
	    $(function () {
	        function getTree() {
	            // Some logic to retrieve, or generate tree structure
				var data = ${tree};

	/*             var data = [{
	                text: "p1",
	                id: '1',
	                state: {
	                    checked: false,
	                  },
	                nodes: [
	                     { text: "p1-1", id: '2' ,state: {checked: true}}, 
	                     { text: "p1-2", id: '3' ,state: {checked: false}}, 
	                     { text: "p1-3", id: '4' ,state: {checked: true}}, 
	                     { text: "p1-4", id: '5', state: {checked: false},
	                    	 nodes: [
	                    	         { text: 'p1-1-1', id: '6'}
	                    	         ]
	                     }]

	            },
	            {
	                text: "p2",
	                id: '7',
	                state: {
	                    checked: false,
	                  }
	            }
	            ] */
	            return data;
	        }
	        $('#tree').treeview({
	            data: getTree(),         // data is not optional
	            multiSelect: true,
	            showCheckbox: true,
				onNodeChecked: function(event, node){
					//alert("选中");
					var selectNodes = getChildNodeIdArr(node); //获取所有子节点
                   if (selectNodes)
                   { //子节点不为空，则选中所有子节点
                       $('#tree').treeview('checkNode', [selectNodes, { silent: false }]);
                   }
					//当全部孩子节点选中时，会自动选中其父节点
                   var parentNode = $("#tree").treeview("getNode", node.parentId);
                   setParentNodeCheck(node);

				},
				onNodeUnchecked: function(event, node){ //取消选中节点
					//alert("未选中");
					var selectNodes = getChildNodeIdArr(node); //获取所有子节点
	                   if(selectNodes)
	                   { //子节点不为空，则取消选中所有子节点
	                       $('#tree').treeview('uncheckNode', [selectNodes, { silent: false }]);
	                   }
				},
	        });

	    })
	    
	    function getChildNodeIdArr(node)
		{
            var ts = [];
            if(node.nodes)
            {
                for(x in node.nodes)
                {
                    ts.push(node.nodes[x].nodeId);
                    if (node.nodes[x].nodes)
                    {
                        var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);
                        for (j in getNodeDieDai)
                        {
                            ts.push(getNodeDieDai[j]);
                        }
                    }
                }
            }
            else
            {
                ts.push(node.nodeId);
            }
            return ts;
        }

        function setParentNodeCheck(node)
        {
            var parentNode = $("#tree").treeview("getNode", node.parentId);
            if (parentNode.nodes)
            {
                var checkedCount = 0;
                for (x in parentNode.nodes)
                {
                    if (parentNode.nodes[x].state.checked)
                    {
                        checkedCount ++;
                    }
                    else
                    {
                        break;
                    }
                }
                if(checkedCount === parentNode.nodes.length)
                {
                    $("#tree").treeview("checkNode", parentNode.nodeId);
                    setParentNodeCheck(parentNode);
                }
            }
        }

        $("#btn-save").click(function (e) {
        	$.blockUI({ message: '<h1>正在加载...</h1>' });
            //获取所有选中了复选框的条目
            var arr = $('#tree').treeview('getChecked');
            //定义一个数组，用于存放所有选中的条目的id
            var checkedIdArray=new Array();
 
            for (var key in arr) {
            	checkedIdArray.push(arr[key].id);
            }
            //去除数组里的空值，因为一级的id设置的空值。
            for(var i = 0 ;i<checkedIdArray.length;i++)
            {
                        if(checkedIdArray[i] == null)
                        {
                        	checkedIdArray.splice(i,1);
                                 i= i-1;
                             
                        }
                         
            }
            //alert(JSON.stringify(checkedIdArray));
            
           if(checkedIdArray.length == 0){
        	   alert("请至少勾选一项维保项目！");
           }else{
               var contractId = ${contract.id};
          	 $.ajax({
          		 url: "/firefighting/contract/checkedId.htm",
                   type: "POST",
                   dataType: "json",
                   data: {
                  	 "checkedIdArray":checkedIdArray,
                 	  	 "contractId":contractId,
                   },
                   traditional: true,//属性在这里设置
                   cache: false,
                   success: function (data)
                   {
                	var hasBuilding = data.hasBuilding;
                	if (hasBuilding == false){
                		alert("请先添加建筑物");
                		$.unblockUI();
                	}else{
                        var canEdite = data.canEdite;
                        if (canEdite == false){
                      	  alert("该合同已有任务下发，无法编辑！");
                        }else{
                      	  var flag = data.flag;
                            if(flag == true)
                            {
                                alert("保存成功！");
                            }
                            else
                            {
                                alert("保存失败，请稍后再试！");
                            }
                        }
                        $.unblockUI();
                	}

                 	 
                   },
                   error:function(data)
                   {
                 	  alert("保存失败，请稍后再试！");
                 	 $.unblockUI();
                   }
               });
           }

        })

	</script>

</body>
</html>